<?php 
class ArticleController extends AuthController{

	private $model;
	public function __init(){
		parent::__init();
		$this->model = K('Article');
	}

	// 显示文章
	public function index(){
		$data = $this->model->getArcCate();
		$this->assign('data',$data);
		$this->display();
	}
	// 添加
	public function add(){
		if(IS_POST){
			//调用模型添加操作
		 	if(!$this->model->addData()){
		 		$this->error($this->model->error);
		 	}   
			$this->success('添加成功',U('index'));
		}
		//处理“所属分类”
		$cateData = K('Category')->all();
		$cateData = Data::tree($cateData, 'cname','cid','pid');
		$this->assign('cateData',$cateData);
		
		//处理文章标签
		$tagData = K('Tag')->all();
		$this->assign('tagData',$tagData);
		
	    $this->display();
	}

	// 删除到回收站
	public function del(){
		$aid = Q('get.aid');
		if($this->model->delData($aid)){
			$this->success('删除文件到回收站');
		}
	}

	// 在回收站真正的删除
	public function realDel(){
		$aid = Q('get.aid');
		if($this->model->realDel($aid)){
			$this->success('删除成功');
		}
	}

	// 编辑
	public function edit(){
		if(IS_POST){
			//从隐藏域获得值
			$aid = Q('post.aid',0,'intval');
		 	if(!$this->model->edit($aid)){
		 		$this->error($this->model->error);
		 	}
			$this->success('修改成功',U('index'));   
		}
		//获得旧数据
		$aid = Q('get.aid',0,'intval');
		$oldData = M()->join("__article__ a JOIN __article_data__ ad ON a.aid=ad.article_aid")->where("aid={$aid}")->find();
		$this->assign('oldData',$oldData);
		//旧数据属性处理分配
		$oldAttr = explode(',', $oldData['attr']);
		$this->assign('oldAttr',$oldAttr);
	
		//处理“所属分类”
		$cateData = K('Category')->all();
		$cateData = Data::tree($cateData, 'cname','cid','pid');
		$this->assign('cateData',$cateData);
		
		//处理“文章标签”
		$tagData = K('Tag')->all();
		$this->assign('tagData',$tagData);
		
		//getField获得指定字段，如果传递第二个参数为true，代表要组成一维数组
		$tagId = K('ArcTag')->where("article_aid={$aid}")->getField('tag_tid',true);
		$this->assign('tagId',$tagId);
		
	    $this->display();
	}

	// 回收站
	public function recycle(){
		$data = $this->model->getRecArticle();
		$this->assign('data',$data);
	    $this->display();
	}
	public function restore(){
		$aid = Q('get.aid');
		if($this->model->restore($aid)){
			$this->success('还原成功');
		}
	}

}


 ?>